from django.contrib.auth.decorators import login_required
class LoginRequiredMixin(object):
  """验证用户是否登录扩展类"""

  @classmethod
  def as_view(cls, **initkwargs):
      # 自定义的as_view()方法中，调用父类的as_view()方法
      view = super().as_view()
      return login_required(view)


from utils.response_code import RETCODE
from django import http
from functools import wraps

def login_required_json(func_view):
    ## 恢复view_func的名字和文档
    @wraps(func_view)
    def wrapper(request,*args,**kwargs):

        if not request.user.is_authenticated:
            return http.JsonResponse({'code': RETCODE.SESSIONERR, 'errmsg': '用户未登录'})
        else:
            return func_view(request,*args,**kwargs)

    return wrapper

class LoginRequiredJSONMixin(object):
    """验证用户是否登陆并返回json的扩展类"""
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        return login_required_json(view)
